package com.system.manager.modules.assit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.entity.PageDTO;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.assit.entity.Notice;
import com.system.manager.modules.assit.entity.NoticeRole;
import com.system.manager.modules.assit.mapper.NoticeMapper;
import com.system.manager.modules.assit.service.INoticeRoleService;
import com.system.manager.modules.assit.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.manager.modules.assit.vo.RoleVo;
import com.system.manager.modules.authorized.entity.Role;
import com.system.manager.modules.authorized.service.IRoleService;
import com.system.manager.modules.authorized.vo.MenuVo;
import com.system.manager.utils.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-25
 */
@Slf4j
@Transactional
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private INoticeRoleService noticeRoleService;


    // 记录当前的公告ID
    private Integer currNoticeId;

    @Override
    public boolean publish(Notice notice) {

        // 1.将通知插入
        log.info("--->1.插入新的公告");
        save(notice);
        currNoticeId = notice.getAId();
        // 1.1查询出角色之间的关系
        List<Role> roleList = roleService.list();
        log.info("--->2.将角色的id pid封装进入menuVo");

        // 2.查询出当前角色的孩子
        // 3.将广告推广给当前角色的孩子
        log.info("--->3.将广告推广给当前角色的孩子");
        try {
            noticeChildren(roleList, notice.getRole());
        } catch (Exception e) {
            e.printStackTrace();
            throw new SysException();
        }

        return true;
    }

    @Override
    public PageDTO<Notice> selectByUId(Integer page, Integer size, Integer id) {
        IPage<Notice> pageNotice = new Page<>(page, size);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        wrapper.orderByDesc("create_time");
        page(pageNotice, wrapper);

        PageDTO<Notice> pageDTO = new PageDTO<>();
        pageDTO.setCount(pageNotice.getTotal());
        pageDTO.setData(pageNotice.getRecords());

        return pageDTO;
    }

    @Override
    public Map<String, Object> selectByRole(Integer role) {

        // 1.查询出【系统公告】
        List<Notice> sysList = new ArrayList<>();

        // 2.查询出【通知公告】
        List<Notice> noticeList = new ArrayList<>();

        // 3.根据当前的角色类型查询出对应的公告
        List<NoticeRole> noticeRoles = selectNoticeRole(role);

        noticeRoles.forEach(noticeRole -> {
            Notice notice = getById(noticeRole.getAId());
            // 3.1判断当前公告的类型，加入不同的公告中
            if(notice.getType() == 0){
                sysList.add(notice);
            }else{
                noticeList.add(notice);
            }
        });

        Map<String, Object> map = new HashMap<>();
        map.put("sysNotice", sysList);
        map.put("normalNotice", noticeList);

        return map;
    }

    /**
     * 根据公告类型查询对应的内容
     * @param type
     * @return
     */
    public List<Notice> selectByType(Integer type){
        return null;
    };


    /**
     * 根据当前的角色类型查询出对应的公告
     * @param role
     * @return
     */
    public List<NoticeRole> selectNoticeRole(Integer role){
        QueryWrapper<NoticeRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", role);
        wrapper.orderByDesc("create_time");
        Page<NoticeRole> noticeRoles = noticeRoleService.
                getBaseMapper().
                selectPage(new Page<NoticeRole>(1, 5),wrapper);

        return noticeRoles.getRecords();
    }

    /**
     * 将公告发布给孩子
     * @param treeList
     * @param pid
     */
    public void noticeChildren( List<Role> treeList ,Integer pid){
        List<Role> retList = new ArrayList<Role>();

        for (Role parent : treeList) {
            if (pid == parent.getPid()) {
                NoticeRole noticeRole = new NoticeRole();
                noticeRole.setRoleId(parent.getRoleId());
                noticeRole.setAId(currNoticeId);
                noticeRoleService.save(noticeRole);
                findChildren(parent, treeList);
            }
        }

    }

    /**
     * 将公告发送给子孙
     * @param parent
     * @param treeList
     */
    public void findChildren(Role parent, List<Role> treeList){
        for (Role child : treeList) {
            if (parent.getRoleId()== child.getPid()) {
                NoticeRole noticeRole = new NoticeRole();
                noticeRole.setRoleId(child.getRoleId());
                noticeRole.setAId(currNoticeId);
                noticeRoleService.save(noticeRole);

                findChildren(child, treeList);
            }
        }
    }
}
